package com.alvin.mobi.web.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.alvin.mobi.domain.Merchandise;
import com.alvin.mobi.service.MerchandiseService;

/**   
 * 
 * @author <a href="mailto:rex2.mj@gmail.com">Leo Meng</a>
 * @date $Date: Jan 16, 2013  $
 */
@Controller
public class MerchandiseController {
	
	@Autowired
	private MerchandiseService merchandiseService;
	
	@RequestMapping(value = "/qr.htm", method = RequestMethod.GET)
	public String merchandise(@RequestParam("qrcode") String qrCode, ModelMap model) {
		Merchandise merchandise = merchandiseService.retrieveGood(qrCode);
		if(merchandise == null){
			merchandise = merchandiseService.retrieveBox(qrCode);
			if(merchandise == null){
				return "redirect:/notfound.htm?qrcode="+qrCode;
			}
		}
		model.addAttribute("merchandise", merchandise);
		return "merchandises";
	}
	
	@RequestMapping(value = "/notfound.htm", method = RequestMethod.GET)
	public String notFound(@RequestParam("qrcode") String qrCode, ModelMap model) {
		model.addAttribute("qrcode", qrCode);
		return "notFound";
	}
	
	@RequestMapping(value = "/ciq.htm", method = RequestMethod.GET)
	public String ciq(@RequestParam(value="id", required=false) String id, ModelMap model) {
		model.addAttribute("id", "CIQ_"+id);
		return "ciq";
	}	
}
